#include <windows.h>
#include <commctrl.h>
#include <windowsx.h>
#include <tchar.h>
#pragma comment(lib, "comctl32")

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

HINSTANCE hInst;

LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;
	WNDCLASS w;

	hInst = hInstance;
	memset(&w, 0, sizeof(WNDCLASS));
	w.style = CS_HREDRAW | CS_VREDRAW;
	w.lpfnWndProc = WndProc;
	w.hInstance = hInst;
	w.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH); //CreateHatchBrush(HS_DIAGCROSS, RGB(255, 0, 0));
	w.lpszClassName = L"My Class";
	w.hCursor = LoadCursor(NULL, IDC_ARROW);
	RegisterClass(&w);

	HWND hWndWindow = CreateWindow(L"My Class", L"My title", WS_OVERLAPPEDWINDOW, 300, 200, 800, 600, NULL, NULL, hInst, NULL);

	ShowWindow(hWndWindow, nCmdShow);
	UpdateWindow(hWndWindow);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	DeleteObject(w.hbrBackground);

	return msg.wParam;
}

WNDPROC StaticWndProc = NULL;
TCHAR szText[] = TEXT("TestString");

LRESULT CALLBACK MyStaticWndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
	if (Message == WM_PAINT)
	{
		RECT rc;
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hwnd, &ps);
		GetClientRect(hwnd, &rc);
		SetBkMode(hdc, TRANSPARENT);
		DrawText(hdc, szText, _tcslen(szText), &rc, DT_CENTER | DT_VCENTER);
		EndPaint(hwnd, &ps);
		return 0;
	}

	//v2 StaticWndProc(hwnd, Message, wparam, lparam);
	return CallWindowProc(StaticWndProc, hwnd, Message, wparam, lparam); //v2
}

HWND hWndStatic; 
HWND hwndButton;

LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
	switch (Message)
	{
		case WM_CREATE:
		{   
			LRESULT lRes = DefWindowProc(hwnd, Message, wparam, lparam);
			hWndStatic = CreateWindowEx(0, L"Static", NULL, WS_CHILD | WS_VISIBLE | SS_LEFT, 10, 130, 200, 40, hwnd, NULL, hInst, NULL); 

			hwndButton = CreateWindow(
				L"BUTTON",  // Predefined class; Unicode assumed 
				L"OK",      // Button text 
				WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
				10,         // x position 
				10,         // y position 
				100,        // Button width
				100,        // Button height
				hwnd,     // Parent window
				NULL,       // No menu.
				(HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
				NULL);
			//StaticWndProc = (WNDPROC)SetWindowLong(hWndStatic, GWL_WNDPROC, (LPARAM)MyStaticWndProc);

			Static_SetText(hWndStatic, TEXT("Static Text"));

			return lRes;
		}

	case WM_DESTROY:
		SetWindowLong(hWndStatic, GWL_WNDPROC, (LPARAM)StaticWndProc); //v2
		PostQuitMessage(0);
		break;

	case WM_COMMAND:
		if (lparam == (LPARAM)hwndButton)
		{
			WORD code = HIWORD(wparam);
			WORD lword = LOWORD(wparam);
			if (BN_CLICKED == code) {
				SetWindowText(hwndButton, TEXT("Clicked"));
			}
		}
		break;

	default:
		return DefWindowProc(hwnd, Message, wparam, lparam);
	}

	return 0;
}